//
//  TYZTableViewController.m
//  TYZ_SC
//
//  Created by 李贤立 on 2017/12/12.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "TYZTableViewController.h"

@interface TYZTableViewController ()
/**
 *  标题文本
 */
@property (nonatomic, strong) TYZLabel *titleLabel;
/**
 *  左边按钮
 */
@property (nonatomic, strong) TYZButton *leftButton;
/**
 *  右边按钮
 */
@property (nonatomic, strong) TYZButton *rightButton;
@end

@implementation TYZTableViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置默认值
    self.isFirstEnter = YES;
    self.titleType = NavigationTitleType_System;
    self.view.backgroundColor = DE_GlobalBackgroundView;
    // 收回键盘
    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTouchInSide)];
    tableViewGesture.numberOfTapsRequired = 1;
    tableViewGesture.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tableViewGesture];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self adjustsFontSize];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.isFirstEnter = NO;
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)dealloc {
    TYZLog(@"%@-%s",self,__FUNCTION__);
}
#pragma mark - actions
- (void)commentTouchInSide{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
- (void)backToPreViewController {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didClickRightButton {
    
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)didClickLeftButton {
    
}
#pragma mark - setter & getter
- (void)setMainTitle:(NSString *)mainTitle {
    _mainTitle = mainTitle;
    self.titleLabel.text = mainTitle;
    [self adjustsFontSize];
}
- (void)setLeftImageName:(NSString *)leftImageName {
    _leftImageName = leftImageName;
    [self.leftButton setBackgroundImage:[UIImage imageNamed:leftImageName] forState:UIControlStateNormal];
}
- (void)setLeftImageHightlightName:(NSString *)leftImageHightlightName {
    _leftImageHightlightName = leftImageHightlightName;
    [self.leftButton setBackgroundImage:[UIImage imageNamed:leftImageHightlightName] forState:UIControlStateHighlighted];
}
- (void)setRightImageName:(NSString *)rightImageName {
    _rightImageName = rightImageName;
    [self.rightButton setBackgroundImage:[UIImage imageNamed:rightImageName] forState:UIControlStateNormal];
}
- (void)setRightImageHightlightName:(NSString *)rightImageHightlightName {
    _rightImageHightlightName = rightImageHightlightName;
    [self.rightButton setBackgroundImage:[UIImage imageNamed:rightImageHightlightName] forState:UIControlStateHighlighted];
}
- (void)setTitleType:(NavigationTitleType)titleType {
    _titleType = titleType;
    switch (titleType) {
        case NavigationTitleType_System:
            [self setNavigationTitle];
            break;
        case NavigationTitleType_Button:
            [self setupNavigationWithTitle:self.mainTitle leftImage:self.leftImageName rightImage:self.rightImageName];
            break;
        case NavigationTitleType_Title:
            [self setupNavigationWithTitle:self.mainTitle];
            break;
        default:
            break;
    }
}
#pragma mark - 私有方法
- (void)setNavigationButton {
    // 左边
    TYZButton *backBtn = [[TYZButton alloc] initWithFrame:CGRectMake(0, 0, DE_NavigationButtonSize, DE_NavigationButtonSize)];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    [backBtn setImage:[UIImage imageNamed:DE_BackButtonImageNormal] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:DE_BackButtonImageHighlighted] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backToPreViewController) forControlEvents:UIControlEventTouchUpInside];
    self.leftButton = backBtn;
    // 右边
    TYZButton *rightButton = [[TYZButton alloc] initWithFrame:CGRectMake(7.5, 7.5, 25, 25)];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.rightButton = rightButton;
}
- (void)setNavigationTitle {
    [self setNavigationButton];
    TYZLabel *titleLabel = [[TYZLabel alloc] initWithFrame:CGRectMake(0, 0, DE_ScreenWidth - 120, 44)];
    self.titleLabel = titleLabel;
    self.navigationItem.titleView = titleLabel;
}
- (TYZView *)setupNavigationTopView {
    TYZView *topView = [[TYZView alloc] initWithFrame:CGRectMake(0, 0, DE_ScreenWidth, DE_NavigationHeight)];
    [self.view addSubview:topView];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:NavigationImage];
    [topView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(topView);
    }];
    return topView;
}
- (void)setupNavigationWithTitle:(NSString *)title {
    TYZView *topView = [self setupNavigationTopView];
    TYZLabel *titleLabel = [[TYZLabel alloc] init];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topView).offset(10);
        make.right.equalTo(topView).offset(-10);
        make.bottom.equalTo(topView).offset(-5);
        make.centerX.equalTo(topView);
    }];
    titleLabel.text = title;
    self.titleLabel = titleLabel;
}
- (void)setupNavigationWithTitle:(NSString *)title leftImage:(NSString *)leftImage rightImage:(NSString *)rightImage {
    TYZView *topView = [self setupNavigationTopView];
    // 左边
    TYZButton *leftButton = [[TYZButton alloc] init];
    [topView addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(topView).offset(-5);
        make.left.equalTo(self.view).offset(DE_NavigationPadding);
        make.width.height.mas_equalTo(DE_NavigationButtonSize);
    }];
    if (leftImage) {
        [leftButton setBackgroundImage:[UIImage imageNamed:leftImage] forState:UIControlStateNormal];
    }
    [leftButton addTarget:self action:@selector(didClickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    self.leftButton = leftButton;
    // 右边
    TYZButton *rightButton = [[TYZButton alloc] init];
    [topView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-DE_NavigationPadding);
        make.centerY.width.height.equalTo(leftButton);
    }];
    if (rightImage) {
        [rightButton setBackgroundImage:[UIImage imageNamed:rightImage] forState:UIControlStateNormal];
    }
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = rightButton;
    // 中间
    TYZLabel *titleLabel = [[TYZLabel alloc] init];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftButton.mas_right);
        make.right.equalTo(rightButton.mas_left);
        make.centerY.equalTo(leftButton);
        make.centerX.equalTo(topView);
    }];
    self.titleLabel = titleLabel;
}
- (void)adjustsFontSize {
    self.titleLabel.text = self.title.length == 0 ? self.mainTitle : self.title;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:DE_NavigationTitleFontSize];
    self.titleLabel.minimumScaleFactor = DE_NavigationTitleMinSizeMultiple;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = DE_NavigationTextColor;
    self.titleLabel.adjustsFontSizeToFitWidth = YES;
}
@end
